<template>
  <div class="product-list-bg">
    <Header />
    <!--    <CategoryMenu />-->
    <section class="search-summary-bar">
      <span v-if="products.length > 0">搜索 <b>“{{ keyword }}”</b>，共 <b>{{ products.length }}</b> 个商品</span>
      <span v-else>暂无匹配的商品</span>
    </section>
    <section class="product-list jd-product-list">
        <ProductCard v-for="item in products" :key="item.id" :product="item" />
    </section>
    <Footer />
  </div>
</template>

<script setup lang="ts">
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import CategoryMenu from '@/components/CategoryMenu.vue'
import ProductCard from '@/components/ProductCard.vue'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getProductList } from '@/api/product.ts'
import { ElMessage } from 'element-plus'

const products = ref([]) //商品列表
const keyword = ref('') //保存关键词，便于显示
const route = useRoute()

onMounted(async () => {
  try {
    keyword.value = (route.query.name as string) || ''
    console.log('搜索关键词:', keyword.value)
    const response = await getProductList({ name: keyword.value })
    console.log('商品列表响应:', response)
    if (response.code === 200 && response.data) {
      // 兼容 records/list
      const rawProducts = response.data.records || response.data.list || response.data
      products.value = rawProducts
      console.log('商品数据:', products.value)
    } else {
      ElMessage.error(response.message || '获取商品列表失败')
    }
  } catch (error) {
    console.error('请求错误:', error)
    ElMessage.error('网络错误，请稍后重试')
  }
})
</script>

<style scoped>
.product-list-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #f0f4ff 0%, #f8fafc 100%);
  width: 100vw;
  overflow-x: hidden;
}
.search-summary-bar {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto 18px auto;
  padding: 18px 24px 8px 24px;
  font-size: 18px;
  color: #222;
  background: #fff;
  border-radius: 0 0 12px 12px;
  box-shadow: 0 2px 8px rgba(120,160,255,0.06);
  font-weight: 500;
  letter-spacing: 1px;
}
.jd-product-list {
  display: flex;
  flex-wrap: wrap;
  gap: 24px 18px;
  margin: 0 auto;
  width: 100%;
  max-width: 1200px;
  justify-content: flex-start;
  padding: 0 16px 32px 16px;
  box-sizing: border-box;
}
.jd-product-list :deep(.product-card) {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(120,160,255,0.10);
  transition: box-shadow 0.22s, transform 0.22s;
  border: 1px solid #e3eaf5;
  min-width: 260px;
  max-width: 340px;
  width: calc(25% - 18px);
  margin-bottom: 0;
  height: auto;
  overflow: hidden;
  position: relative;
}
.jd-product-list :deep(.product-card):hover {
  box-shadow: 0 8px 32px rgba(80,120,255,0.16);
  transform: translateY(-4px) scale(1.025);
  z-index: 2;
}
@media (max-width: 900px) {
  .jd-product-list {
    gap: 18px 10px;
  }
  .jd-product-list :deep(.product-card) {
    min-width: 180px;
    max-width: 48vw;
    width: calc(50% - 10px);
  }
}
@media (max-width: 600px) {
  .search-summary-bar {
    font-size: 15px;
    padding: 12px 8px 6px 8px;
  }
  .jd-product-list {
    gap: 10px 4px;
    padding: 0 2px 16px 2px;
  }
  .jd-product-list :deep(.product-card) {
    min-width: 120px;
    max-width: 98vw;
    width: 100%;
    border-radius: 8px;
  }
}
</style>
